package com.springboot.service.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.dao.SystemMenuDao;
import com.springboot.dao.SystemRoleDao;
import com.springboot.dao.SystemUserRoleDao;
import com.springboot.entity.SystemMenuEntity;
import com.springboot.entity.SystemRoleEntity;
import com.springboot.returndata.dto.AddUpdateRoleDto;
import com.springboot.returndata.dto.GetRoleListDto;
import com.springboot.returndata.vo.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.util.common.TreeVoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author zyj
 * @since 2023-02-24
 */
@Service
@Slf4j
public class SystemRoleService extends ServiceImpl<SystemRoleDao, SystemRoleEntity> {

    @Resource
    private SystemRoleDao systemRoleEntityDao;

    @Resource
    private SystemMenuDao systemMenuEntityDao;

    @Resource
    private SystemUserRoleDao systemUserRoleDao;

    /**
     * 获取角色列表
     *
     * @param getRoleListDto
     * @return
     */
    public Page<SystemRoleVO> getRoleList(GetRoleListDto getRoleListDto) {
        Page<SystemRoleVO> roleList = systemRoleEntityDao.getRoleList(getRoleListDto.getPage(), getRoleListDto);
        return roleList;
    }
    /**
     * 获取角色列表
     *
     * @param
     * @return
     */
    public List<SystemRoleVO> getRoleListAll() {
        List<SystemRoleVO> roleList = systemRoleEntityDao.getRoleListAll();
        return roleList;
    }


    /**
     * description:
      * @param roleId
     * @return: RoleResourceVo
     * @author: zyj
     * @date: 2023/3/1
     */
    public List<RoleResourceVo> getRoleResourceTree(Integer roleId){
        //获取资源类型
        List<SystemMenuEntity> allMenuByType = systemMenuEntityDao.getAllMenuByType(null);

        //获取角色资源
        List<Integer> roleMenus = systemMenuEntityDao.getRoleMenu(roleId);

        //转化为树列表
        List<RoleResourceVo> roleResourceVos = new ArrayList<>();
        for(SystemMenuEntity systemMenuEntity: allMenuByType){
            RoleResourceVo commonTreeVO = new RoleResourceVo();
            BeanUtils.copyProperties(systemMenuEntity, commonTreeVO);
            Integer id = systemMenuEntity.getId();
            commonTreeVO.setId(id.toString());
            commonTreeVO.setParentId(systemMenuEntity.getPid().toString());
            if(roleMenus.contains(id)){
                commonTreeVO.setChecked(true);
            }else {
                commonTreeVO.setChecked(false);
            }

            roleResourceVos.add(commonTreeVO);
        }

        //转化为树结构
        List<RoleResourceVo> treeVoCommon = TreeVoUtil.getListTreeVoCommon(roleResourceVos, "1", TreeVoUtil.key_children);

        return treeVoCommon;
    }

    /**
     * description: 添加角色
      * @param addUpdateResourceDto
     * @return: void
     * @author: zyj
     * @date: 2023/3/3
     */
    public void addRole(AddUpdateRoleDto addUpdateResourceDto){
        SystemRoleEntity systemRoleEntity = new SystemRoleEntity();
        systemRoleEntity.setRoleName(addUpdateResourceDto.getRoleName());
        systemRoleEntity.setRoleStatus(addUpdateResourceDto.getRoleStatus());

        systemRoleEntityDao.insert(systemRoleEntity);
    }


    /**
     * description: 修改角色
     * @param addUpdateResourceDto
     * @return: void
     * @author: zyj
     * @date: 2023/3/3
     */
    public void updateRole(AddUpdateRoleDto addUpdateResourceDto){
        systemRoleEntityDao.updateById(addUpdateResourceDto.getRoleId(), addUpdateResourceDto.getRoleName(), addUpdateResourceDto.getRoleStatus());
    }

    /**
     * description: 删除角色
      * @param roleId
     * @return: void
     * @author: zyj
     * @date: 2023/3/3
     */
    @Transactional(rollbackFor = Exception.class)
    public void  deleteRole(Integer roleId){
        //删除角色
        systemRoleEntityDao.deleteById(roleId);

        //删除用户所对应的角色
        systemUserRoleDao.deleteByRoleId(roleId);
    }
}

